package com.hy.${packageCode}.shared.distribute.counter;

import lombok.RequiredArgsConstructor;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 单实例优化，基于 JDK AtomicLong 实现
 */
@RequiredArgsConstructor
public class StandaloneCounter implements DistCounter {
    private final AtomicLong delegatingCounter;

    @Override
    public long get() {
        return delegatingCounter.get();
    }

    @Override
    public void set(long initValue) {
        delegatingCounter.set(initValue);
    }

    @Override
    public long getAndSet(long setValue) {
        return delegatingCounter.getAndSet(setValue);
    }

    @Override
    public boolean compareAndSet(long expectedValue, long newValue) {
        return delegatingCounter.compareAndSet(expectedValue, newValue);
    }

    @Override
    public long increase(long delta) {
        return delegatingCounter.addAndGet(delta);
    }
}
